package com.compass.doctor.ui.chat.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.netease.yunxin.kit.common.ui.databinding.ContentPopListViewBinding;
import com.netease.yunxin.kit.common.ui.databinding.ContentPopListViewWithShadowBinding;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Description： 弹窗Pop
 * Date:2023/12/13 19:21
 */
public class CustomListPopView extends PopupWindow {
    private final LinearLayout llGroup;

    public CustomListPopView(Context context, List<Item> itemList, Config config) {
        super(context);
        FrameLayout rootView;
        Object backgroundView;

        ContentPopListViewBinding binding = ContentPopListViewBinding.inflate(LayoutInflater.from(context));
        rootView = binding.getRoot();
        this.llGroup = binding.llGroup;
        backgroundView = this.llGroup;

        if (config.bgColor != null) {
            ((View)backgroundView).setBackgroundColor(config.bgColor);
        }

        if (config.bgRes != null) {
            ((View)backgroundView).setBackgroundResource(config.bgRes);
        }

        this.initUI(itemList);
        this.setContentView(rootView);
        this.setWidth(-2);
        this.setHeight(-2);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(0));
    }

    private void initUI(List<Item> itemList) {
        if (itemList != null && !itemList.isEmpty()) {
            Iterator var2 = itemList.iterator();

            while(var2.hasNext()) {
                Item item = (Item)var2.next();
                if (item != null) {
                    item.itemView.setOnClickListener((v) -> {
                        if (item.clickListener != null) {
                            item.clickListener.onClick(v);
                        }

                        if (item.autoDismissWhenClicked && this.isShowing()) {
                            this.dismiss();
                        }

                    });
                    this.llGroup.addView(item.itemView, item.params);
                }
            }

        }
    }

    public static class Item {
        private final View itemView;
        private final View.OnClickListener clickListener;
        private final boolean autoDismissWhenClicked;
        private final LinearLayout.LayoutParams params;

        public Item(View itemView, LinearLayout.LayoutParams params, View.OnClickListener clickListener) {
            this(itemView, params, clickListener, true);
        }

        public Item(View itemView, LinearLayout.LayoutParams params, View.OnClickListener clickListener, boolean autoDismissWhenClicked) {
            this.itemView = itemView;
            this.params = params;
            this.clickListener = clickListener;
            this.autoDismissWhenClicked = autoDismissWhenClicked;
        }

        public static class Builder {
            private View itemView;
            private View.OnClickListener clickListener;
            private boolean autoDismissWhenClicked = true;
            private LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-2, -2);

            public Builder() {
            }

            public Item.Builder configView(View view) {
                this.itemView = view;
                return this;
            }

            public Item.Builder configClickListener(View.OnClickListener clickListener) {
                this.clickListener = clickListener;
                return this;
            }

            public Item.Builder configAutoDismissWhenClicked(boolean autoDismissWhenClicked) {
                this.autoDismissWhenClicked = autoDismissWhenClicked;
                return this;
            }

            public Item.Builder configParams(LinearLayout.LayoutParams params) {
                this.params = params;
                return this;
            }

            public Item build() {
                return new Item(this.itemView, this.params, this.clickListener, this.autoDismissWhenClicked);
            }
        }
    }

    public static class Config {
        public Integer bgColor;
        public Integer bgRes;
        public boolean enableShadow = true;

        public Config() {
        }
    }

    public static class Builder {
        private final Context context;
        private final List<Item> itemList = new ArrayList();
        private final Config config = new Config();

        public Builder(Context context) {
            this.context = context;
        }

        public Builder addItem(Item item) {
            this.itemList.add(item);
            return this;
        }

        public Builder backgroundColor(int color) {
            this.config.bgColor = color;
            return this;
        }

        public Builder backgroundRes(int resId) {
            this.config.bgRes = resId;
            return this;
        }

        public Builder enableShadow(boolean enable) {
            this.config.enableShadow = enable;
            return this;
        }

        public CustomListPopView build() {
            return new CustomListPopView(this.context, this.itemList, this.config);
        }
    }
}
